package leetcode.D200.T122;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-02-22 15:02:45 星期二
 */
class Solution {
    // 一刷
    /*public int maxProfit(int[] prices) {
        int dp0 = 0, dp1 = Integer.MIN_VALUE;
        for(int i=0; i<prices.length; ++i) {
            int temp = Math.max(dp0, dp1+prices[i]);
            dp1 = Math.max(dp1, dp0-prices[i]);
            dp0 = temp;
        }
        return dp0;
    }*/

    // 二刷
    public int maxProfit(int[] prices) {
        int dp0 = 0, dp1 = -prices[0];
        for(int i=0; i<prices.length; ++i) {
            int temp = Math.max(dp0, dp1+prices[i]);
            dp1 = Math.max(dp1, dp0-prices[i]);
            dp0 = temp;
        }
        return dp0;
    }
}
